<?php

/*
 * cui:phpstorm
 * 2021/7/21
 * 15:53
 * User: s1mple_qcloud@163.com
 *
 */

namespace App\Api\Controllers;


use App\Api\Models\CmcPromotionPintuanGroup;
use App\Api\Models\Member;
use App\Api\Repositories\GroupingRepositorie;
use App\Api\Services\JsonService;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class GroupShareController extends Controller
{
    // 用户拼团或参与拼团
    public function insertGroup(Request $request)
    {
        // 获取用户id
        $id = $request->get('member_id');
        // 获取拼团id
        $combinationId = $request->get('group_id');
        // 判断是不是用户
        if (!Member::findMember($id)){
            return JsonService::fail('您不是用户，请先注册','',config('code.HTTP_NO_AUTH'));
        }else{
            // 获取到当前拼团组的参与次数
            $count = CmcPromotionPintuanGroup::getPintuanCount($id);
            $memberId = CmcPromotionPintuanGroup::pinkInfo($combinationId);
            $grouping = GroupingRepositorie::Have($memberId);
            // 判断帮助拼团用户是否参加
            if ($count >= 1){
                return JsonService::fail('当前活动已经参与过，请勿重复参与','',config('code.HTTP_ALREADY_EXISTED'));
            }else{
                $bcadd = CmcPromotionPintuanGroup::add_join_count($combinationId);
                if ($bcadd){
                    return JsonService::success('参与成功',$grouping,config('code.HTTP_OK'));
                }else{
                    return JsonService::fail('参与失败','',config('code.HTTP_INVALID_PARAMETER'));
                }
            }
        }
    }


    // 查看当前用户的拼团活动
    public function getMemberJoinPink(Request $request,$id)
    {
        $id = $request->get('member_id');
        $pinkId = $request->get('pintuan_id');
        if (!$id || !is_numeric($id)) return JsonService::fail('系统繁忙','',config('code.HTTP_INVALID_PARAMETER'));
        if (!$pinkId || !is_numeric($pinkId)) return JsonService::fail('系统繁忙,稍后再试','',config('code.HTTP_INVALID_PARAMETER'));
    }



}